home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c,comp.unix.programmer
- Path: howland.reston.ans.net!torn!sq!msb
- From: msb@sq.com (Mark Brader)
- Subject: Re: Q: '\n' character
- Message-ID: <1996Apr11.192937.25676@sq.com>
- Organization: SoftQuad Inc., Toronto, Canada
- References: <4kj66f$k0o@ren.cei.net>
- Date: Thu, 11 Apr 1996 19:29:37 GMT
-
- > > Is there a function or some sort of way that I could remove '\n'
- > > charecter form the end of the string.
- >
- > fgets(buffer, file);
- > buffer[strlen(buffer)-1]=0;
- >
- > It may not be the fastest, but it is darned near the easiest.
-
- Or the buggiest. Instead use:
-
- len = strlen (buffer);
- if (len > 0 && buffer[len-1] == '\n') buffer[len-1] = '\0';
-
- Both tests in the "if" are necessary.
-
- In the specific case of a string obtained from fgets(), we can be
- sure that if there is a newline then it is the last character.
- This leads to the alternative approach:
-
- ptr = strchr (buffer, '\n'); /* or strrchr() */
- if (ptr) *ptr = '\0';
- --
- Mark Brader, msb@sq.com, SoftQuad Inc., Toronto
- "I'm a little worried about the bug-eater," she said. "We're embedded
- in bugs, have you noticed?" -- Niven, "The Integral Trees"
-
- My text in this article is in the public domain.
-